/**
 * 
 */
package org.xlab.baseframework.listener;

import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.event.SystemCodeChangedEvent;
import org.xlab.baseframework.system.SystemRuntimeEnvironment;


/**
 * @author MaKuBEX
 * 
 */
@Component
public class SystemCodeChangedListener implements
		ApplicationListener<SystemCodeChangedEvent> {
	private static final Logger log = Logger
			.getLogger(SystemCodeChangedListener.class);
	@Autowired(required = true)
	@Qualifier("bpf_systemCodeServiceMessageSource")
	private MessageSource messageSource;

	/**
	 * @return the messageSource
	 */
	public MessageSource getMessageSource() {
		return messageSource;
	}

	/**
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	public void onApplicationEvent(SystemCodeChangedEvent event) {
		String message = null;
		this.messageSource
				.getMessage("info.system.event.syscdchanged", new Object[] {
						event.getCode().getCode(),
						event.getCode().getDescription() }, Locale.getDefault());
		log.debug(message);
		if (EnumDeletedIndentifying.Y == event.getCode().getDeletedInd())
			SystemRuntimeEnvironment.removeCode(event.getCode().getCode());
		else
			SystemRuntimeEnvironment.setCode(event.getCode());
	}

	/**
	 * @param messageSource
	 *            the messageSource to set
	 */
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

}
